import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';

class StockMaterialcheckOdooApi {
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
          model: 'stock.materialcheck',
          method: 'search_read',
          kwargs: recordsKwargs.toMap());
      var total = await OdooHttpUtil.callKw(
          model: 'stock.materialcheck',
          method: 'search_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> warePage({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      //PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
        domain: domainList,
        //  limit: pageModel.size,
        //offset: (pageModel.current - 1) * pageModel.size,
        //order: pageModel.orders[0].column! +
        //  " " +
        //(pageModel.orders[0].asc! ? "asc" : "desc")
      );
      var records = await OdooHttpUtil.callKw(
          model: 'stock.quant',
          method: 'stock_inquiry',
          kwargs: recordsKwargs.toMap());
      // var total = await OdooHttpUtil.callKw(
      //     model: 'stock.materialcheck',
      //     method: 'search_count',
      //     kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      // pageModel.records = list;
      // pageModel.total = total;
      // pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: list);
    } catch (e) {
      print(e);
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'stock.materialcheck', method: "unlink", args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> saveOrUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      if (data["id"] == null) {
        await OdooHttpUtil.callKw(
            model: 'stock.materialcheck', method: 'to_create', args: [data]);
      } else {
        await OdooHttpUtil.callKw(
            model: 'stock.materialcheck',
            method: 'write',
            args: [
              [data["id"]],
              data
            ]);
      }
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> save(data, list) async {
    ResponseBodyApi responseBodyApi;
    try {
      List productList = [];
      list.forEach((element) {
        Map<String, dynamic> map = {
          'warehouse_id': int.parse(data['warehouse_id']),
          'material_id': element['product_tmpl_id'][0],
          'stockqty': element['quantity']
        };
        productList.add(map);
      });
      var fieldsinfo = {
        'stock.materialcheckitem': ['check_id'],
        'data': productList
      };
      await OdooHttpUtil.callKw(
          model: 'stock.materialcheck',
          method: 'create',
          args: [data],
          fieldsInfo: fieldsinfo);

      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> confirm(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs recordsKwargs = Kwargs(
        domain: data,
        //  limit: pageModel.size,
        //offset: (pageModel.current - 1) * pageModel.size,
        //order: pageModel.orders[0].column! +
        //  " " +
        //(pageModel.orders[0].asc! ? "asc" : "desc")
      );
      await OdooHttpUtil.callKw(
        model: 'stock.picking',
        method: 'confirm_transfer',
        kwargs: recordsKwargs.toMap(),
      );

      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> profitLoss(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs recordsKwargs = Kwargs(
        domain: data,
      );
      var result = await OdooHttpUtil.callKw(
        model: 'stock.materialcheck',
        method: 'profit_loss',
        kwargs: recordsKwargs.toMap(),
      );

      responseBodyApi = ResponseBodyApi(success: true,data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }
}
